﻿using System;
using System.Collections;
using System.Text;
using System.Configuration;
using System.Xml;

namespace YEA.Infrastructure.Gateway.Config
{
    public enum ExchangeType { direct, topic, fanout };
    public class Exchange : ConfigurationElement
    {
        [ConfigurationProperty("name", DefaultValue = "", IsRequired = true)]
        public string Name
        {
            get { return (string) this["name"]; }
            set { this["name"] = value; }
        }
        [ConfigurationProperty("connection", DefaultValue = "", IsRequired = true)]
        public string Connection
        {
            get { return (string)this["connection"]; }
            set { this["connection"] = value; }
        }
        [ConfigurationProperty("type", DefaultValue = "direct", IsRequired = true)]
        public ExchangeType Type
        {
            get { return (ExchangeType)this["type"]; }
            set { this["type"] = value; }
        }
        [ConfigurationProperty("autodelete", DefaultValue= true, IsRequired = false)]
        public bool AutoDelete
        {
            get { return (bool)this["autodelete"]; }
            set { this["autodelete"] = value; }
        }
        [ConfigurationProperty("durable", DefaultValue = false, IsRequired = false)]
        public bool Durable
        {
            get { return (bool)this["durable"]; }
            set { this["durable"] = value; }
        }
    }
    public class ExchangeCollection : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new Exchange();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((Exchange)element).Name;
        }
        public new string AddElementName
        {
            get { return base.AddElementName; }
            set { base.AddElementName = value; }
        }
        public new string ClearElementName
        {
            get { return base.ClearElementName; }
            set { base.ClearElementName = value; }

        }

        public new string RemoveElementName
        {
            get { return base.RemoveElementName; }
        }

        public new int Count
        {
            get { return base.Count; }
        }


        public Exchange this[int index]
        {
            get
            {
                return (Exchange)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        new public Exchange this[string Name]
        {
            get { return (Exchange)BaseGet(Name); }
        }

        public int IndexOf(Exchange exchange)
        {
            return BaseIndexOf(exchange);
        }

        public void Add(Exchange exchange)
        {
            BaseAdd(exchange);
            // Add custom code here.
        }

        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
            // Add custom code here.
        }

        public void Remove(Exchange exchange)
        {
            if (BaseIndexOf(exchange) >= 0)
                BaseRemove(exchange.Name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Clear()
        {
            BaseClear();
            // Add custom code here.
        }
    }
    public class AMQPObjectsDeclarationSection : ConfigurationSection
    {
        [ConfigurationProperty("ExchangeList", IsDefaultCollection = false)]
        public ExchangeCollection ExchangeList
        {
            get
            {
                ExchangeCollection exchanges = (ExchangeCollection)this["ExchangeList"];
                return exchanges;
            }
        }
        [ConfigurationProperty("QueueList", IsDefaultCollection = false)]
        public QueueCollection QueueList
        {
            get
            {
                QueueCollection queue = (QueueCollection)this["QueueList"];
                return queue;
            }
        }
        [ConfigurationProperty("BindingList", IsDefaultCollection = false)]
        public BindingCollection BindingList
        {
            get
            {
                BindingCollection binding = (BindingCollection)this["BindingList"];
                return binding;
            }
        }

        protected override void DeserializeSection(System.Xml.XmlReader reader)
        {
            base.DeserializeSection(reader);
            // You can add custom processing code here.
        }

        protected override string SerializeSection(
            ConfigurationElement parentElement,
            string name, ConfigurationSaveMode saveMode)
        {
            string s =
                base.SerializeSection(parentElement,
                name, saveMode);
            // You can add custom processing code here.
            return s;
        }

    }
}
